﻿using System;
using System.ComponentModel;
using System.Text.Json.Serialization;
using System.Windows.Media.Imaging;
using WWLauncher.Constant;
using WWLauncher.Startup;
using WWLauncher.Util;
using static System.Windows.Forms.LinkLabel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;


namespace WWLauncher.ViewModel
{
    /// <summary>
    /// 图标信息
    /// </summary>
    [Serializable]
    public class IconInfo : INotifyPropertyChanged
    {
        private string targetPath; //路径
        private string name; //文件名
        private int count = 0; //打开次数
        [field: NonSerialized]
        private BitmapImage bitmapImage; //位图
        private byte[] _imageByteArr; //图片 byte数组
        [field: NonSerialized]
        private string startArg; //启动参数
        private string lnkPath;
        private string relativePath; //相对路径 
        private string toolTip;
        private IconStartType startType;
        private bool _capsLockOn;
        private bool startup;
        private Guid id;
        private Guid linkId;

        public IconInfo()
        {
        }
        public IconInfo(IconInfo icon)
        {
            this.Name = icon.Name;
            this.TargetPath = icon.TargetPath;
        }
        public Guid Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                OnPropertyChanged(nameof(Id));
            }
        }
        public string RelativePath
        {
            get
            {
                return relativePath;
            }
            set
            {
                relativePath = value;
                OnPropertyChanged("RelativePath");
            }
        }

        public string LnkPath
        {
            get
            {
                return lnkPath;
            }
            set
            {
                lnkPath = value;
                OnPropertyChanged("LnkPath");
            }
        }

        public string StartArg
        {
            get
            {
                return startArg;
            }
            set
            {
                startArg = value;
                OnPropertyChanged("StartArg");
            }
        }

        public int Count
        {
            get
            {
                return count;
            }
            set
            {
                count = value;
                OnPropertyChanged("Count");
                OnPropertyChanged("ToolTip");
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }


        public string TargetPath
        {
            get
            {
                return targetPath;
            }
            set
            {
                targetPath = value;
                OnPropertyChanged("TargetPath");
            }
        }
        public Guid LinkId
        {
            get
            {
                return linkId;
            }
            set
            {
                linkId = value;
                OnPropertyChanged(nameof(LinkId));
            }
        }

        [JsonIgnore]
        public BitmapImage BitmapImage
        {
            get
            {

                return ImageUtil.ByteArrToImage(ImageByteArr);
            }
            set
            {
                bitmapImage = value;
                ImageByteArr = ImageUtil.BitmapImageToByte(bitmapImage);
                OnPropertyChanged("BitmapImage");
            }
        }


        public byte[] ImageByteArr
        {
            get
            {
                return _imageByteArr;
            }
            set
            {
                _imageByteArr = value;
                OnPropertyChanged("ImageByteArr");
            }
        }
        /// <summary>
        /// 开启大写
        /// </summary>
        public bool CapsLockOn
        {
            get
            {
                return _capsLockOn;
            }
            set
            {
                _capsLockOn = value;
                OnPropertyChanged("CapsLockOn");
            }
        }
        public bool IsStartup
        {
            get => startup;
            set
            {
                startup = value;
                OnPropertyChanged(nameof(IsStartup));
            }
        }
        [JsonIgnore]
        public string ToolTip
        {
            get
            {
                if (TargetPath.IsNullOrEmpty())
                {
                    toolTip = $"名称：{Name}\n路径：\n使用次数：{Count}";
                }
                else
                {
                    toolTip = $"名称：{Name}\n路径：{TargetPath}\n使用次数：{Count}";
                }

                return toolTip;
            }
            set
            {
                toolTip = value;
                OnPropertyChanged("ToolTip");
            }
        }

        public IconStartType StartType
        {
            get => startType;
            set
            {
                startType = value;
                OnPropertyChanged("StartType");
            }
        }


        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}